<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>权限</title>
<jsp:include page="/WEB-INF/backstage/util.jsp"/>
<link rel="stylesheet" href="${pageContext.request.contextPath }/js/ztree/demo.css" />
	<link rel="stylesheet" href="${pageContext.request.contextPath }/js/ztree/zTreeStyle.css" type="text/css">
	<script type="text/javascript" src="${pageContext.request.contextPath }/js/ztree/jquery-1.4.4.min.js"></script>
	<script type="text/javascript" src="${pageContext.request.contextPath }/js/ztree/jquery.ztree.core.js"></script>
	<script type="text/javascript" src="${pageContext.request.contextPath }/js/ztree/jquery.ztree.excheck.js"></script>
	<SCRIPT type="text/javascript">
		var setting = {
			check: {
				enable: true
			},
			data: {
				simpleData: {
					enable: true
				}
			}
		};

		var zNodes =${data};
		
		$(document).ready(function(){
			$.fn.zTree.init($("#treeDemo"), setting, zNodes);
		});
		
		$(function(){
			$.ajax({
				type:"post",
				url:"${pageContext.request.contextPath }/role/querySysRole",
				success:function(msg){
					$.each(msg,function(index,sysRole){
						var option="<option value="+sysRole.role_id+">"+sysRole.role_name+"</option>";
						$("#role_id").append(option);
						if('${role_id}'!=''){
							$("#role_id").val('${role_id}');
						}
					});
				}
			});
			
			 $("#role_id").change(function(){
				var role_id=$("#role_id").val();
				document.forms[0].action="${pageContext.request.contextPath }/menu/selectPermission2?role_id="+role_id;
				document.forms[0].submit();
			}); 
		});
	</SCRIPT>
</HEAD>


<BODY>
<form method="post" id="myForm">
<div class="form-inline">
		<div class="form-group">
			<label for="role_id">角色:</label> <select id="role_id" name="role_id"
				class="form-control">
			</select>
		</div>
	<div class="zTreeDemoBackground left">
		<ul id="treeDemo" class="ztree"></ul>
	</div><br/><br/>
		<button type="button" class="btn btn-primary" onclick="addPermission()">
			<span class="glyphicon glyphicon-search" id="refersh"></span>保存权限
		</button>
	</div>
	</form>
</BODY>
<script>
	//添加权限
	function addPermission(){
		//初始化树
		var zTree = $.fn.zTree.getZTreeObj("treeDemo");
		var nodes=zTree.getCheckedNodes();  //获取复选框改变的值
		var role_id=$("#role_id").val();
		var array=[];
		$.each(nodes,function(index,n){
			array.push(n.id);
		});
		var data=array.join(",");
		 $.ajax({
		    	url:"${pageContext.request.contextPath}/menu/savePermission",
		    	type:"post",
		    	data:{"role_id":role_id,"data":data},
		    	success:function(msg){
		    		alert("保存成功");
		    		//合闭树
		    		var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
		    		treeObj.expandAll(false);
		    		parent.location.reload();
		    	}
		     });   
	}
</script>
</html>